package top.yifengxiner.blockchain.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.yifengxiner.blockchain.mapper.DiplomaMapper;
import top.yifengxiner.blockchain.pojo.entity.Certificate;
import top.yifengxiner.blockchain.pojo.entity.Diploma;
import top.yifengxiner.blockchain.pojo.vo.CertificateVo;
import top.yifengxiner.blockchain.pojo.vo.IdVo;
import top.yifengxiner.blockchain.service.BlockChainService;
import top.yifengxiner.blockchain.service.DiplomaService;
import top.yifengxiner.blockchain.service.EducatedService;
import top.yifengxiner.common.core.pojo.AjaxResult;
import top.yifengxiner.framework.shiro.service.SysPasswordService;

import java.util.Date;
import java.util.List;

/**
 * @author ：baixiao
 * @date ：Created in 2021/5/12 16:38
 * @description：学位证书服务
 * @modified By：
 * @version: 0.0.1$
 */
@Service
public class DiplomaServiceImpl implements DiplomaService
{

    @Autowired
    private DiplomaMapper diplomaMapper;

    @Autowired
    private EducatedService educatedService;

    @Autowired
    private SysPasswordService passwordService;

    @Autowired
    private BlockChainService blockChainService;

    @Override
    public List<Diploma> getDiploma(IdVo idVo)
    {
        QueryWrapper<Diploma> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",idVo.getId());
        return diplomaMapper.selectList(queryWrapper);
    }

    @Override
    public Diploma getDiplomaInfo(IdVo idVo)
    {
        QueryWrapper<Diploma> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("diploma_id",idVo.getId());
        return diplomaMapper.selectOne(queryWrapper);
    }

    @Override
    public int createDiploma(CertificateVo certificateVo)
    {
        Diploma diploma = new Diploma();
        diploma.setUserId(certificateVo.getEducatedId());
        System.out.println(certificateVo.getEducatedId());
        diploma.setUsername(educatedService.getEducatedById(certificateVo.getEducatedId()).getUsername());
        diploma.setDiplomaName(certificateVo.getEduStage());
        diploma.setIssueTime(new Date());
        diploma.setEduInstitution(certificateVo.getEducatorName());

        String hash = passwordService.encryptPassword(certificateVo.getEduStage()
                ,certificateVo.getEducatorName()
                ,educatedService.getEducatedById(certificateVo.getEducatedId()).getIdentity());

        List<Diploma> list = diplomaMapper.selectList(new QueryWrapper<Diploma>());
        for(Diploma diploma1 : list)
        {
            if(hash.equals(diploma1.getHashcode()))
            {
                return -1;
            }
        }

        diploma.setHashcode(hash);
        diploma.setExistFlag('0');
        int res = diplomaMapper.insert(diploma);

        if(res == 1)
        {
            Certificate certificate = new Certificate();
            certificate.setExistFlag("true");
            certificate.setEducatorName(certificateVo.getEducatorName());
            certificate.setHashcode(hash);
            certificate.setIdentity(educatedService.getEducatedById(certificateVo.getEducatedId()).getIdentity());
            int re = blockChainService.CertificateInvoke(certificate);
            if(re == 1)
            {
                System.out.println("******上链成功******");
            }
            else
            {
                System.out.println("******上链失败******");
            }
        }
        return res;
    }

    @Override
    public AjaxResult checkDiploma(String id)
    {
        Certificate certificate = new Certificate();
        Diploma diploma = diplomaMapper.selectById(Long.parseLong(id));
        certificate.setHashcode(diploma.getHashcode());
        certificate.setEducatorName(diploma.getEduInstitution());
        certificate.setIdentity(educatedService.getEducatedById(diploma.getUserId()).getIdentity());
        int res = blockChainService.CertificateQuery(certificate);
        if(res == 1)
        {
            return AjaxResult.success();
        }
        else if(res == -1)
        {
            AjaxResult.warn("证书不存在");
        }
        return AjaxResult.error();
    }
}
